﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class Controls_News_ctrNewsCategory : System.Web.UI.UserControl
{
    private int nCurrentLanguage = Support.GetCurrentLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        if (!IsPostBack)
        {
            this.InitData(1);
        }
    }
    private void initDataPaging()
    {
        Pager.MaxSmartShortCutCount = 6;
        Pager.NormalModePageCount = 15;
        Pager.SmartShortCutRatio = 3;
        Pager.SmartShortCutThreshold = 30;
        Pager.ItemCount = 0;
        Pager.RTL = false;

        Pager.PageSize = 22;
        Pager.BackToFirstClause = "Về trang đầu";
        Pager.CompactModePageCount = 0;
        Pager.CurrentIndex = 1;
        Pager.FirstClause = "&lt;&lt;";
        Pager.FromClause = "From";
        Pager.GenerateFirstLastSection = false;
        Pager.GenerateGoToSection = false;
        Pager.GenerateHiddenHyperlinks = false;
        Pager.GeneratePagerInfoSection = true;
        Pager.GenerateSmartShortCuts = true;
        Pager.GenerateToolTips = true;

        Pager.GoClause = "go";
        Pager.GoToLastClause = "tới trang cuối";
        Pager.LastClause = "&gt;&gt;";
        Pager.NextClause = "&gt;";
        Pager.NextToPageClause = "Đến trang";
        Pager.OfClause = "trong tổng số";
        Pager.PageClause = "Trang";
        Pager.PreviousClause = "&lt;";
        Pager.QueryStringParameterName = "pagerControlCurrentPageIndex";
        Pager.ShowResultClause = "Từ sản phẩm";
        Pager.ToClause = "đến";
    }

    private void InitData(int CurrentPageIndex)
    {
        int nPageSize = 10;
        int nCat = ConvertData.ConvertToInt(Request.QueryString[Config.NEWS_CAT]);
        int nPageCount = 0;
        try
        {
            DataTable dtb = new DataTable();
            News objNews = new News();
            dtb = objNews.Search(0, 0, "", nCat, Support.GetDefaultLanguage(), Support.GetCurrentLanguage(), EnumeType.ACTIVE, CurrentPageIndex, nPageSize, ref nPageCount);
            dtlNewsByCat.DataSource = dtb;
            dtlNewsByCat.DataBind();
            if (nCat > 0)
            {
                NewsCategory objCat = new NewsCategory();
                objCat.LoadById(nCat);
                switch (nCurrentLanguage)
                {
                    case 1:
                        lblNews.Text = ConvertData.ConvertToString(objCat.Data.Description1).ToUpper();
                        break;
                    case 2:
                        lblNews.Text = ConvertData.ConvertToString(objCat.Data.Description2).ToUpper();
                        break;
                    case 3:
                        lblNews.Text = ConvertData.ConvertToString(objCat.Data.Description3).ToUpper();
                        break;
                }
            }
            else
            {
                lblNews.Text = MultipleLanguage.NEWS[nCurrentLanguage - 1].ToUpper();  
            }
            if (nPageCount > 0)
                tblWarning.Visible = false;
            else
                tblWarning.Visible = true;

            Pager.Visible = true;
            VisiblePaging(nPageCount);
            Pager.ItemCount = nPageCount;
        }
        catch
        {
            Response.Redirect("~/Default.aspx");
        }
    }
    protected void dtlNewsByCat_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            ImageButton btnImage = (ImageButton)e.Item.FindControl("btnImage");
            HyperLink lnkTitle = (HyperLink)e.Item.FindControl("lnkTitle");
            Label lnkInitData = (Label)e.Item.FindControl("lnkInitData");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "NewsID"));
            lnkTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsTitle"));
            string sInit = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "InitContent"));
            if (sInit.Length >150)
            {sInit = ConvertData.TruncateString(sInit,150) + "...";  }
            lnkInitData.Text = sInit;
            lnkTitle.NavigateUrl = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nID;
         
            btnImage.ImageUrl = Config.ROOT + Config.IMAGE_NEWS_DEFAULT_UPLOAD + ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Image"));
            btnImage.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "NewsID"));
        }
    }
    protected void dtlNewsByCat_ItemCommand(object source, DataListCommandEventArgs e)
    {
        string sCommand = e.CommandName;
        int nNewsID = ConvertData.ConvertToInt(e.CommandArgument);
        switch (sCommand)
        {
            case "newsimage":
                string sNewsDetail = Config.ROOT + Pages.FrontEnds.NEWS_DETAIL + "?" + Config.NEWS_ID + "=" + nNewsID;
                Response.Redirect(sNewsDetail);
                break;
        }
    }

    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager.CurrentIndex = currnetPageIndx;
        this.InitData (currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = 10;
        if (intPagecount > nPageSize)
            Pager.Visible = true;
        else
            Pager.Visible = false;
    }
    #region Comment Code
    //PageSize="10" BackToFirstClause="Về trang đầu"
    //BackToPageClause="Trở về trang" CompactModePageCount="0" CurrentIndex="1" FirstClause="&lt;&lt;"
    //FromClause="From" GenerateFirstLastSection="False" GenerateGoToSection="False"
    //GenerateHiddenHyperlinks="False" GeneratePagerInfoSection="True" GenerateSmartShortCuts="True"
    //GenerateToolTips="True" GoClause="go" GoToLastClause="tới trang cuối" ItemCount="0"

    //LastClause="&gt;&gt;" MaxSmartShortCutCount="6" NextClause="&gt;" NextToPageClause="Đến trang"
    //NormalModePageCount="15" OfClause="" PageClause="Trang" PreviousClause="&lt;"
    //QueryStringParameterName="pagerControlCurrentPageIndex" RTL="False" ShowingResultClause="Viser"
    //ShowResultClause="" SmartShortCutRatio="3" SmartShortCutThreshold="30"
    //ToClause="đến"
    #endregion
}
